#!/bin/sh

##########################################################################
# THIS MODULE ALLOWS YOU TO SEE THE MAIN INFORMATION ON THE INSTALLED APPS
##########################################################################

_files_header() {
	echo ""
	APPSNUMB=$(find "$APPSPATH" -type f -name 'remove' -exec grep -L 'usr/local/lib' {} \; 2>/dev/null | sed -r 's|/[^/]+$||' | sort | uniq | wc -l)
	if [ "$APPSNUMB" = 1 ]; then
		APPSMESSAGE="YOU HAVE INSTALLED $APPSNUMB PROGRAM"
	else
		APPSMESSAGE="YOU HAVE INSTALLED $APPSNUMB PROGRAMS"
	fi
	if grep -q 'usr/local/lib' $APPSPATH/*/remove 2> /dev/null; then
		LIBNUMB=$(grep -l "usr/local/lib" "$APPSPATH"/*/remove | wc -l)
		if [ "$LIBNUMB" = 1 ]; then
			LIBSMESSAGE="AND $LIBNUMB LIBRARY"
		else
			LIBSMESSAGE="AND $LIBNUMB LIBRARIES"
		fi
		echo "- $APPSMESSAGE $LIBSMESSAGE MANAGED BY $(echo "\"$AMCLI\"" | tr a-z A-Z)"
	else
		echo "- $APPSMESSAGE MANAGED BY $(echo "\"$AMCLI\"" | tr a-z A-Z)"
	fi
	echo ""
}

_files_sizes() {
	if grep -q "usr/local/lib" ./$arg/remove; then
		LIBNAME=$(cat $APPSPATH/$arg/remove | tr ' ' '\n' | grep "usr/local/lib" | head -1)
		SIZE=$(du -sh $LIBNAME | cut -f1 | sort -rh | head -1)
	else
		SIZE=$(du -sh -- $arg | cut -f1 -d"	")
	fi
	SIZE=$(echo "$SIZE" | sed 's/.$/ &/; s/$/iB/')
	echo " ◆ $arg	|	$SIZE" >> "$AMCACHEDIR"/files-sizes
}

_files_if_binary() {
	if file ./"$arg"/* | grep "static" >/dev/null 2>&1; then
		echo " ◆ $arg	|	static-binary" >> "$AMCACHEDIR"/files-type
	else
		echo " ◆ $arg	|	dynamic-binary" >> "$AMCACHEDIR"/files-type
	fi
}

_files_if_library() {
	echo " ◆ $arg	|	library" >> "$AMCACHEDIR"/files-type
}

_files_if_script() {
	if grep "/ffwa" "${LINK:-$FILE}" >/dev/null 2>&1; then
		echo " ◆ $arg	|	launcher" >> "$AMCACHEDIR"/files-type
	elif grep '#!.*bash' "${LINK:-$FILE}" >/dev/null 2>&1; then
		echo " ◆ $arg	|	bash-script" >> "$AMCACHEDIR"/files-type
	elif grep '#!.*dash' "${LINK:-$FILE}" >/dev/null 2>&1; then
		echo " ◆ $arg	|	dash-script" >> "$AMCACHEDIR"/files-type
	elif grep '#!.*bin.*sh' "${LINK:-$FILE}" >/dev/null 2>&1; then
		echo " ◆ $arg	|	posix-script" >> "$AMCACHEDIR"/files-type
	elif grep '#!.*awk' "${LINK:-$FILE}" >/dev/null 2>&1; then
		echo " ◆ $arg	|	awk-script" >> "$AMCACHEDIR"/files-type
	else
		echo " ◆ $arg	|	script" >> "$AMCACHEDIR"/files-type
	fi
}

_files_if_appimage() {
	if ! echo "$string" | grep 'AppImages require FUSE to run' >/dev/null 2>&1; then
		if grep "SANDBOXDIR" "$FILE" >/dev/null 2>&1; then
			echo " ◆ $arg	|	appimage3🔒" >> "$AMCACHEDIR"/files-type
		else
			echo " ◆ $arg	|	appimage3" >> "$AMCACHEDIR"/files-type
		fi
	else
		if grep "SANDBOXDIR" "$(command -v "$arg")" >/dev/null 2>&1; then
			echo " ◆ $arg	|	appimage2🔒" >> "$AMCACHEDIR"/files-type
		else
			echo " ◆ $arg	|	appimage2" >> "$AMCACHEDIR"/files-type
		fi
	fi
}

_files_type() {
	APPVERSION=$(cat "$AMCACHEDIR"/version-args | grep -w " ◆ $arg	|" | sed 's:.*|	::')
	string=$(strings -d ./"$arg/$arg" 2>/dev/null)
	FILE=$(command -v "$arg" 2>/dev/null)
	LINK=$(readlink "$FILE" 2>/dev/null)
	if echo "$string" | grep -- '--appimage-extract option' >/dev/null 2>&1; then
		_files_if_appimage
	elif file ./"$arg"/* | grep -E 'LSB|/bin' >/dev/null 2>&1; then
		_files_if_binary
	elif grep "#!" "${LINK:-$FILE}" >/dev/null 2>&1; then
		_files_if_script
	elif grep  "usr/local/lib" ./"$arg"/remove >/dev/null 2>&1; then
		_files_if_library
	else
		echo " ◆ $arg	|	other" >> "$AMCACHEDIR"/files-type
	fi
}

_files_files() {
	cd $APPSPATH &&
	INSTALLED_APPS=$(find -name 'remove' -printf "%h\n" 2>/dev/null | du -sh -- * 2> /dev/null | sort -rh | sed 's@.*	@@')
	if ! test -f "$AMCACHEDIR"/version-args; then
		_check_version
	fi
	if ! test -f "$AMCACHEDIR"/files-type; then
		for arg in $INSTALLED_APPS; do
			if test -f ./$arg/remove 2>/dev/null; then
		 		_files_type
			fi
		done
	fi
	rm -f "$AMCACHEDIR"/files-sizes
	for arg in $INSTALLED_APPS; do
		if test -f ./$arg/remove 2>/dev/null; then
			_files_sizes
		fi
	done
}

_files() {
	_files_files
	rm -f "$AMCACHEDIR"/files-args
	INSTALLED_APPS=$(find -name 'remove' -printf "%h\n" 2>/dev/null | du -sh -- * 2>/dev/null | sort -rh | sed 's@.*	@@')
	for arg in $INSTALLED_APPS; do
		if test -f ./$arg/remove 2>/dev/null; then
			APPVERSION=$(grep -w " ◆ $arg	|" "$AMCACHEDIR"/version-args | tr '	' '\n' | tail -1)
			APPTYPE=$(grep -w " ◆ $arg	|" "$AMCACHEDIR"/files-type | tr '	' '\n' | tail -1)
			APPSYZE=$(grep -w " ◆ $arg	|" "$AMCACHEDIR"/files-sizes | tr '	' '\n' | tail -1)
			echo " ◆ $arg	|	$APPVERSION	|	$APPTYPE	|	$APPSYZE" >> "$AMCACHEDIR"/files-args
		fi
	done
}

_files_show_only_number() {
	find "$APPSPATH" -type f -name 'remove' 2>/dev/null | uniq | wc -l
	exit 0
}

_files_total_size() {
	if [ "$(_files_show_only_number)" = "0" ]; then
		printf "\n TOTAL SIZE: $(du -ch $(find -type d -name 'modules' -printf "%h\n") 2>/dev/null | awk 'END {print $1"iB"}' | sed 's/...$/ &/') of disk space in use\n\n"
	else
		command -v aisap >/dev/null 2>&1 && printf '\n%s\n' " AppImages with 🔒 are sandboxed with aisap"
		printf "\n TOTAL SIZE: $(du -ch $(find -type f -name 'remove' -printf "%h\n") 2>/dev/null | awk 'END {print $1"iB"}' | sed 's/...$/ &/') of disk space in use\n\n"
	fi
}

_files_sort_by_name() {
	_files_header
	rm -f "$AMCACHEDIR"/files-args-byname
	_files
	printf '%s\n' "- APPNAME | VERSION | TYPE | SIZE " >> "$AMCACHEDIR"/files-args-byname
	printf '%s\n' "- ------- | ------- | ---- | ----" >> "$AMCACHEDIR"/files-args-byname
	sort "$AMCACHEDIR"/files-args 2>/dev/null >> "$AMCACHEDIR"/files-args-byname
	column -t "$AMCACHEDIR"/files-args-byname
	_files_total_size
}

_files_sort_by_size() {
	_files_header
	rm -f "$AMCACHEDIR"/files-args-bysize
	_files
	printf '%s\n' "- APPNAME | VERSION | TYPE | SIZE " >> "$AMCACHEDIR"/files-args-bysize
	printf '%s\n' "- ------- | ------- | ---- | ----" >> "$AMCACHEDIR"/files-args-bysize
	cat "$AMCACHEDIR"/files-args >> "$AMCACHEDIR"/files-args-bysize 2>/dev/null
	column -t "$AMCACHEDIR"/files-args-bysize
	_files_total_size
}

if [ "$2" = "--less" ]; then
	_files_show_only_number
elif [ "$2" = "--byname" ]; then
	_files_sort_by_name
else
	_files_sort_by_size
fi
